//
//  HomePageSearchResultTableViewCell.m
//  Taste
//
//  Created by 徐昊 on 16/9/23.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "HomePageSearchResultTableViewCell.h"
#import "HomePageSearchResultContent.h"
#import "UIImageView+WebCache.h"
#import "NSString+Vali.h"

@interface HomePageSearchResultTableViewCell ()
@property (weak, nonatomic) IBOutlet UIImageView *pic;
@property (weak, nonatomic) IBOutlet UILabel *title;
@property (weak, nonatomic) IBOutlet UILabel *shortContent;

@end

@implementation HomePageSearchResultTableViewCell

- (void)setCid:(NSInteger)cid {
    _cid = cid;
}
- (void)setSearchString:(NSString *)searchString {
    _searchString = searchString;
}
- (void)setContent:(HomePageSearchResultContent *)content {
    _content = content;
    [_pic sd_setImageWithURL:[NSURL URLWithString:[NSString imageWebWithStr:_content.pic]] placeholderImage:[UIImage imageNamed:@"IMG_Universal_Placeholder"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    }];
    if (_cid == 1) {
        NSString *string = [NSString stringWithFormat:@"<font color='red'>%@</font>", _searchString];
        NSString *searchString = [_content.content stringByReplacingOccurrencesOfString:string withString:_searchString];
        _title.text = _content.title;
        _shortContent.text = searchString;
    } else if (_cid == 14) {
        _title.text = _content.topmenu_name;
        _shortContent.text = _content.short_content;
    } else if (_cid == 16) {
        _title.text = _content.title;
        _shortContent.text = _content.short_content;
    }
    NSRange range = [_shortContent.text rangeOfString:_searchString];
    NSRange range2 = [_title.text rangeOfString:_searchString];
    [self setTextColor:_title FontNumber:[UIFont systemFontOfSize:17] AndRange:range2 AndColor:[UIColor redColor]];
    [self setTextColor:_shortContent FontNumber:[UIFont systemFontOfSize:15] AndRange:range AndColor:[UIColor redColor]];
}
//设置不同字体颜色
-(void)setTextColor:(UILabel *)label FontNumber:(id)font AndRange:(NSRange)range AndColor:(UIColor *)vaColor
{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:label.text];
    //设置字号
    [str addAttribute:NSFontAttributeName value:font range:range];
    //设置文字颜色
    [str addAttribute:NSForegroundColorAttributeName value:vaColor range:range];
    
    label.attributedText = str;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
